﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.IO;
using System.Windows.Forms;

namespace weiqijiance.温湿度大气压
{
     public class wsddqyweike:wsddqybass
     {
         /// <summary>
         /// 打开串口
         /// </summary>
         /// <param name="sSet1">串口设置</param>
         /// <returns>是否成功打开</returns>
         public override bool openCOM(string sSet1)
         {
             bool breturn = base.openCOM(sSet1);
             commex.ReadTimeout = 500;
             commex.ReceivedBytesThreshold = 11;//达到11字节以后才开始触发串口
             commex.DataReceived += comGAS_DataReceived;
             return breturn;
         }

         public override bool SendDeviceCmd()
         {
             byte[] cCmdBuf = new byte[8] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
             cCmdBuf[0] = 0x01;
             cCmdBuf[1] = 0x03;
             cCmdBuf[2] = 0x00;
             cCmdBuf[3] = 0x00;//
             cCmdBuf[4] = 0x00;//
             cCmdBuf[5] = 0x03;//
             cCmdBuf[6] = 0x05;//
             cCmdBuf[7] = 0xCB;//
             if (commex.IsOpen)
             {
                 Byte[] m_byte = new Byte[8];
                 for (int i = 0; i < 8; i++)
                 {
                     m_byte[i] = cCmdBuf[i];
                 }
                 commex.Write(m_byte, 0, 8);
             }
             return true;
         }
         bool bqs = false;
         bool bgetjzz = false;
         public override bool setjianding(float wd, float sd, float dqy)
         {
             bqs = true;
             bgetjzz = false;
             float fwdcz = wd - m_fWenDu;
             float fsdcz = sd - m_fShiDu;
             float fdqycz = dqy - m_fYaLi;
             long lwdz = (long)(fwdcz * 10f);
             long lsdz = (long)(fsdcz * 10f);
             long ldqyz = (long)(fdqycz * 10f);

             //SendDevicebd(0x01,0x03,0x00,0x37,0x00,0x03);
             DateTime dtime = DateTime.Now;
             while (true)
             {
                 SendDevicebd(0x01, 0x03, 0x00, 0x37, 0x00, 0x03);
                 System.Threading.Thread.Sleep(800);
                 if (bgetjzz)
                 {
                     break;
                 }
                 if (dtime.AddSeconds(2) < DateTime.Now)
                 {
                     bqs = false;
                     return false;
                 }
             }
             wdjz = ConvertComplementCode(wdjz);
             sdjz = ConvertComplementCode(sdjz);
             yljz = ConvertComplementCode(yljz);
             lwdz = lwdz + wdjz;
             lsdz = lsdz + sdjz;
             ldqyz = ldqyz + yljz;
             lwdz = ConvertComplementCode(lwdz);
             lsdz = ConvertComplementCode(lsdz);
             ldqyz = ConvertComplementCode(ldqyz);
             //01 06 00 37 00 0A B8 03 
             SendDevicebd(0x01, 0x06, 0x00, 0x37, (byte)(lwdz / 256), (byte)(lwdz % 256));
             System.Threading.Thread.Sleep(1000);
             SendDevicebd(0x01, 0x06, 0x00, 0x38, (byte)(lsdz / 256), (byte)(lsdz % 256));
             System.Threading.Thread.Sleep(1000);
             SendDevicebd(0x01, 0x06, 0x00, 0x39, (byte)(ldqyz / 256), (byte)(ldqyz % 256));
             bqs = false;
             return true;
         }
         void comGAS_DataReceived(object sender, SerialDataReceivedEventArgs e)
         {
             if (commex.IsOpen)
             {
                 System.Threading.Thread.Sleep(300);
                 int n = commex.BytesToRead;//
                 byte[] buf1 = new byte[n];//声明一个临时数组存储当前来的串口数据  
                 commex.Read(buf1, 0, n);
                 if (n > 10)
                 {
                     // 01 03 00 00 00 03 05 CB 
                     //01 03 06 02 59 01 2E 03 DE DD C7 
                     for (int i = 0; i < n - 10; i++)
                     {
                         byte[] bjy = new byte[9];
                         for (int z = 0; z < 9; z++)
                         {
                             bjy[z] = buf1[z];
                         }
                         byte[] bjyms = weiqijiance.CCRC16Class.GetCRC16(bjy, true);
                         if (!(bjyms[0] == buf1[i + 9] && bjyms[1] == buf1[i + 10]))
                         {
                             continue;
                         }
                         if (buf1[0 + i] == 0x01 && buf1[1 + i] == 0x03 && buf1[2 + i] == 0x06)
                         {
                             #region 取数
                             if (!bqs)
                             {
                                 //  m_fWenDu, m_fShiDu, m_fYaLi
                                 float.TryParse(string.Format("{0}", (buf1[5 + i] * 256 + buf1[6 + i]) * 0.1), out m_fWenDu);
                                 float.TryParse(string.Format("{0}", (buf1[3 + i] * 256 + buf1[4 + i]) * 0.1), out m_fShiDu);
                                 float.TryParse(string.Format("{0}", (buf1[7 + i] * 256 + buf1[8 + i]) * 0.1), out m_fYaLi);
                                 break;
                             }
                             #endregion
                             #region 取校准值
                             //01 03 00 37 00 03 B4 05 
                             //01 03 06 00 0A 00 00 00 00 B9 74 (
                             else
                             {
                                 //  m_fWenDu, m_fShiDu, m_fYaLi
                                 long.TryParse(string.Format("{0}", (buf1[3 + i] * 256 + buf1[4 + i])), out wdjz);
                                 long.TryParse(string.Format("{0}", (buf1[5 + i] * 256 + buf1[6 + i])), out sdjz);
                                 long.TryParse(string.Format("{0}", (buf1[7 + i] * 256 + buf1[8 + i])), out yljz);
                                 bgetjzz = true;
                                 break;
                             }
                             #endregion
                         }
                     }
                     //01 06 00 37 00 0A B8 03 
                     #region 设置校准值
                     #endregion
                 }
             }
         }
    }
}
